package de.zinulla.moviethek;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchArrayAdapter extends ArrayAdapter<String> {
	private final Activity context;
	private final String[] searchTitel;
	private final String[] releaseDates;
	private final Drawable[] cover;

	static class ViewHolder {
		public TextView titel;
		public TextView releasedate;
		public ImageView image;
	}
 
	public SearchArrayAdapter(Activity context, String[] searchTitel, String[] releaseDates, Drawable[] cover) {
		super(context, R.layout.search_results, searchTitel);
		this.context = context;
		this.searchTitel = searchTitel;
		this.releaseDates = releaseDates;
		this.cover = cover;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.search_results, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.titel = (TextView) rowView.findViewById(R.id.resulttitel);
			viewHolder.releasedate = (TextView) rowView.findViewById(R.id.resultreleasedate);
			viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		String titel = searchTitel[position];
		String releasedate = releaseDates[position];
		Drawable coverImage = cover[position];
		holder.titel.setText(titel);
		holder.releasedate.setText(releasedate);
		holder.image.setImageDrawable(coverImage);

		return rowView;
	}
}